import 'package:get/get.dart';

import '../../../utils/xin_he_toast.dart';
import 'xin_he_forget_pwd_state.dart';

class XinHeForgetPwdLogic extends GetxController {
  final XinHeForgetPwdState state = XinHeForgetPwdState();

  //新密码改变
  void onNewPasswordChange(String text) {
    state.pw = text;
    setSaveButtonStatus();

  }

  //新密码改变
  void onSurePasswordChange(String text) {
    state.surePw = text;
    setSaveButtonStatus();

  }

  void onPhoneChange(String text){
    state.phone = text;
    setSaveButtonStatus();
  }

  //验证码文本框改变
  void onCodeChange(String text) {
    state.code = text;
    // if(state.code.length == 6){
    //   state.codeSendSuccess.value = true;
    // }else{
    //   state.codeSendSuccess.value = false;
    // }
    setSaveButtonStatus();

  }

  ///设置状态
  setSaveButtonStatus() {
    if(state.pw.length<8 || state.surePw.length<8 || state.code.length <6 || state.phone.length!=11 ){
      state.saveEnable.value = false;
    }else{
      state.saveEnable.value = true;
    }

    if(state.pw.length<8 || state.surePw.length<8){
      state.codeEnable.value = false;
    }else {
      state.codeEnable.value = true;

    }
  }

  String getPhoneString() {
    if('13800001111'.length != 11 ) return '';

    return '验证码已发送到您的${getPhoneNum('13800001111')}手机号';
  }

  ///手机号中间4位加密
  String getPhoneNum(String phone ){
    if(phone.isNotEmpty){
      return  phone.replaceFirst(new RegExp(r'\d{4}'), '****', 3);

    }
    return '';
  }

  ///获取短信验证码点击事件
  Future<bool> clickGetCode() async {

    if(!GetUtils.isPhoneNumber(state.phone)){
      XinHeToast.center('手机号格式错误!');
      return false;
    }
    // if (state.pw.isEmpty) {
    //   XinHeToast.center('请输入密码');
    //   return false;
    // }
    // if (state.pw.length < 8 || state.pw.length > 15 ) {
    //   XinHeToast.center('请输入8到15位数字密码组合');
    //   return false;
    // }
    // if (state.surePw.isEmpty) {
    //   XinHeToast.center('请输入确认密码');
    //   return false;
    // }
    //
    // if (state.pw != state.surePw) {
    //   XinHeToast.center('两次密码不一致');
    //   return false;
    // }


    Map<String, dynamic> params = {
      "phone": '13800001111',
      'smsCodeType':'RESET',
      "countryCode": '+86',
    };

    return true;
    // KDReqDataResult data = await KDHttpReqData.baseUniversalPost(
    //     url: KDUrlClass.sendCode, params: params, isPost: true);
    // if (data.result) {
    //   state.codeSendSuccess.value = true;
    //   XinHeToast.center('验证码发送成功');
    //   return true;
    // } else {
    //   XinHeToast.center(data.data['errorMsg']);
    //   return false;
    // }
  }

  /// 保存更改的密码
  Future<bool> savePassword() async {
    if(!GetUtils.isPhoneNumber('13800001111')){
      XinHeToast.center('手机号格式错误!');
      return false;
    }
    if (state.pw.isEmpty) {
      XinHeToast.center('请输入密码');
      return false;
    }
    if (state.pw.length < 8 || state.pw.length > 15 ) {
      XinHeToast.center('请输入8到15位数字密码组合');
      return false;
    }
    if (state.surePw.isEmpty) {
      XinHeToast.center('请输入确认密码');
      return false;
    }

    if (state.pw != state.surePw) {
      XinHeToast.center('两次密码不一致');
      return false;
    }



    Map<String, dynamic> params = {
      'smsCode':state.code,
      "password": state.surePw,
    };
    // await KDHttpReqData.baseUniversalPost(
    //     url: KDUrlClass.updatePassword,
    //     params: params,
    //     isPost: true,
    //     requestSuccessEvent: (data) async{
    //       if(data['code'] == 200) {
    //         XinHeToast.center('重置成功');
    //         //重置成功，刷新用户数据
    //         kdUserData.userDetailInfo();
    //
    //         Get.back();
    //         return true;
    //       }
    //       return false;
    //
    //     },
    //     requestErrorEvent: (errorMsg) async {
    //       XinHeToast.center(errorMsg);
    //       return true;
    //     }
    // );

    return true;

  }
}
